﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;

using API.Models;
using API.Models.Repository;

namespace API.Controllers.api
{
    public class IncomingInvoicesController : BasicController
    {
        //[Queryable]
        public virtual IQueryable<V_IncomingInvoices> Get()
        {
            var repository = new IncomingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var data = repository.Find().AsQueryable();
            return data;
        }

        public virtual V_IncomingInvoices Get(Guid id)
        {
            var repository = new IncomingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var data = repository.Find(r => r.GUID_RECORD == id);
            return data.FirstOrDefault();
        }

        /// <summary>
        /// Установка номера
        /// </summary>
        /// <param name="id"></param>
        /// <param name="number"></param>
        public void Post(V_IncomingInvoices value)
        {
            var repository = new INVOICES_Repository();
            repository.SetSession(this.currentSession);

            var entity = repository.Find(r => r.GUID_RECORD == value.GUID_RECORD).FirstOrDefault();
            entity.CUSTOMER_DOC_ID = value.DOC_ID;

            repository.Edit(entity);
            repository.Save();
        }

        [HttpDelete]
        public virtual void Delete(Guid id)
        {
            var repository = new IncomingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var entity = repository.Find(r => r.GUID_RECORD == id).FirstOrDefault();
            repository.Delete(entity);
            repository.Save();
        }
    }
}
